// 用于处理退保申请

<template>
  <div id="Process">
    <el-dialog
      title="退保详情"
      :visible.sync="dialogVisible"
      width="50%"
      center
    >
      <div id="det">
        <ul>
          <li>订单编号：{{ data._id }}</li>
          <li>客户编号：{{ data.customersId }}</li>
          <li>订单时间：{{ data.orderTIme }}</li>
          <template v-if="data.insuresID">
            <li>保险产品：{{ data.insuresID.name }}</li>
            <li>保险对象：{{ data.insuresID.suitableFor }}</li>
            <li>金额：{{ data.insuresID.price }}</li>
            <li>付款方式：{{ data.insuresID.payType }}</li>
            <li>
              状态：
              <el-tag v-if="data.isWarranty == 1">退保中</el-tag>
              <el-tag v-else-if="data.isWarranty == 2" type="success"
                >退保成功</el-tag
              >
              <el-tag v-else type="danger">退保失败</el-tag>
            </li>
          </template>
        </ul>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button
          v-if="data.isWarranty == 1 && data.orderState == 2"
          type="primary"
          @click="comfirm(data._id)"
          >同意</el-button
        >
        <el-button
          v-if="data.isWarranty == 1 && data.orderState == 2"
          type="warning"
          @click="disagree(data._id)"
          >拒绝</el-button
        >
        <el-button @click="cancel()">取 消</el-button>
      </span>
    </el-dialog>

    <h1 class="title">退保申请</h1>
    <template>
      <el-table stripe :data="insuresorders" border style="width: 100%">
        <el-table-column type="index" fixed align="center"></el-table-column>
        <el-table-column fixed prop="_id" label="订单编号" width="220px">
        </el-table-column>
        <el-table-column prop="customersId" label="客户编号" width="220px">
        </el-table-column>
        <el-table-column label="保单类型" width="100px">
          <template slot-scope="scope" width="100px">
            {{ scope.row.insuresID.policy.join(" / ") }}
          </template>
        </el-table-column>
        <el-table-column label="保险产品" width="100px">
          <template slot-scope="scope">
            {{ scope.row.insuresID.name }}
          </template>
        </el-table-column>
        <el-table-column label="付款方式" width="100px">
          <template slot-scope="scope">
            {{ scope.row.insuresID.payType }}
          </template>
        </el-table-column>

        <el-table-column label="订单状态" width="100px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.orderState == 1">待支付</el-tag>
            <el-tag type="success" v-if="scope.row.orderState == 2"
              >已支付</el-tag
            >
            <el-tag type="danger" v-if="scope.row.orderState == 3"
              >已取消</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column label="状态" width="100px">
          <template slot-scope="scope">
            <template v-if="scope.row.orderState == 2">
              <el-tag v-if="scope.row.isWarranty == 1">退保中</el-tag>
              <el-tag v-else-if="scope.row.isWarranty == 2" type="success"
                >退保成功</el-tag
              >
              <el-tag v-else type="danger">退保失败</el-tag>
            </template>
            <template v-else>订单已取消</template>
          </template>
        </el-table-column>

        <el-table-column label="保险对象" width="100px">
          <template slot-scope="scope">
            {{ scope.row.insuresID.suitableFor }}
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="details(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageData.currentPage + 1"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
  "insuresorders"
);

export default {
  computed: {
    ...mapState(["insuresorders", "pageData"]),
  },
  methods: {
    ...mapActions(["getAllOrders", "updateOrder"]),
    ...mapMutations(["changePageSize", "changeCurrentPage"]),
    //操作成功提示
    success() {
      this.$message({
        message: "操作成功",
        type: "success",
      });
    },

    //操作失败
    fail() {
      this.$message({
        message: "操作失败",
        type: "warning",
      });
    },

    //查看详情
    details(data) {
      this.data = data;
      this.dialogVisible = true;
    },

    //每页数据条数
    handleSizeChange(val) {
      this.changePageSize(val);
      this.getAllOrders();
    },

    //翻页
    handleCurrentChange(val) {
      this.changeCurrentPage(val - 1);
      this.getAllOrders();
    },

    //拒绝
    async disagree(_id) {
      const { status } = await this.updateOrder({
        _id,
        update: { isWarranty: 3 },
      });
      if (status) {
        this.success();
        this.getAllOrders();
      } else {
        this.fail();
      }
      this.close();
    },

    async comfirm(_id) {
      const { status } = await this.updateOrder({
        _id,
        update: { isWarranty: 2 },
      });
      if (status) {
        this.success();
        this.getAllOrders();
      } else {
        this.fail();
      }
      this.close();
    },

    cancel() {
      console.log("取消");
      this.close();
    },
    close() {
      this.dialogVisible = false;
    },
  },

  data() {
    return {
      data: {},
      dialogVisible: false,
    };
  },

  async created() {
    this.getAllOrders();
  },
};
</script>

<style lang="scss" scoped>
#Process {
  .title {
    font-size: 18px;
    font-weight: lighter;
    margin: 10px 0;
  }
  #det {
    ul > li {
      padding: 10px 0;
    }
  }
}
</style>